package demo.client.run;

import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;

import core.util.StringUtil;
import demo.client.socket.DemoClient;

public class ClientRun {
	private String serverAddress;
	private int port;
	private long timeIdle;
	public int numberClient = 0;
	public static void main(String[] args) {
		ClientRun clientRun = new ClientRun();
		clientRun.loadConfig();
		clientRun.creatClient(clientRun.numberClient);
	}
	
	public void loadConfig(){
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.loadConfig();
		Configuration config = ConfigurationManager.getConfiguration("ClientConfig.xml");
		this.serverAddress = config.getProperty("SERVER_ADDRESS", "localhost", "SOCKET");
		
		String portString = config.getProperty("PORT", "1234", "SOCKET");
		if (StringUtil.isCastToLong(portString)) {
			this.port = new Integer(portString);
		}
		
		String timeString = config.getProperty("TIME_IDLE", "0", "SOCKET");
		if (StringUtil.isCastToLong(timeString)) {
			this.timeIdle = new Long(timeString);
		}
		
		String numberClientString = config.getProperty("NUMBER_CLIENT", "1", "SOCKET");
		if (StringUtil.isCastToInteger(numberClientString)) {
			numberClient = new Integer(numberClientString);
		}
	}
	
	public void creatClient(int i){
		ClientLog.loadLog();
		for (int j = 0; j < i; j++) {
			String name = "Client" + j;
			DemoClient demoClient = new DemoClient(serverAddress, port, name);
			demoClient.setNewTimeIdle(timeIdle);
			demoClient.start();
		}
	}
}
